/*
 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva.
 *
 * This file may be distributed under the terms of the Q Public License
 * as defined by Trolltech AS of Norway and appearing in the file
 * LICENSE.QPL included in the packaging of this file.
 *
 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

package net.sf.fmj.utility;

import java.util.Vector;
import java.util.logging.Logger;

import javax.media.Manager;
import javax.media.PackageManager;

/**
 * Used to help recognize when JMF is in the classpath ahead of FMJ.
 * 
 * 
 *
 */
public final class ClasspathChecker
{

	private static final Logger logger = LoggerSingleton.logger;

	public static boolean checkAndWarn()
	{
		boolean result = true;
		
		if (!checkFMJPrefixInPackageManager())
		{
			logger.warning("net.sf.fmj not found in PackageManager.getContentPrefixList() and PackageManager.getProtocolPrefixList(); is JMF ahead of FMJ in the classpath?");
			result = false;
		}
		
		if (checkJMFInClassPath())
		{	logger.info("JMF detected in classpath");
		}
		
		if (!checkManagerImplementation())
		{
			logger.warning("javax.media.Manager is JMF's implementation, not FMJ's; is JMF ahead of FMJ in the classpath?");
			result = false;
		}
		
		logger.info("javax.media.Manager version: " + Manager.getVersion());
		
		
		return result;
		
	}
	
	public static boolean check()
	{
		boolean result = true;
		
		if (!checkFMJPrefixInPackageManager())
		{
			//logger.warning("net.sf.fmj not found in PackageManager.getContentPrefixList() and PackageManager.getProtocolPrefixList(); is JMF ahead of FMJ in the classpath?");
			result = false;
		}
		
		if (!checkManagerImplementation())
		{
			//logger.warning("javax.media.Manager is JMF's implementation, not FMJ's; is JMF ahead of FMJ in the classpath?");
			result = false;
		}
		
		return result;
	}
	
	public static boolean checkFMJPrefixInPackageManager()
	{
		final Vector contentList = PackageManager.getContentPrefixList();
		if (!contentList.contains("net.sf.fmj")) 
			return false;
		
		final Vector protocolList = PackageManager.getProtocolPrefixList();
		if (!protocolList.contains("net.sf.fmj")) 
			return false;
		
		return true;
		
	}
	
	/**
	 * 
	 * @return true if FMJ manager, false if JMF.
	 */
	public static boolean checkManagerImplementation()
	{
		try
		{
			Manager.class.getField("FMJ_TAG");
			return true;
		}
		catch (Exception e)
		{	return false;
		}
	}
	
	/**
	 * Check if JMF is in the classpath (regardless of position).
	 * @return true if JMF is in the classpath.
	 */
	public static boolean checkJMFInClassPath()
	{
		try
		{
			// just try a few:
			Class.forName("com.sun.media.BasicClock");
			Class.forName("com.sun.media.BasicCodec");
			Class.forName("com.sun.media.BasicConnector");
			return true;
		}
		catch (Exception e)
		{
			return false;
		}
	}
}
